﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_DataLayer;
using Gem_Tools_Utility;

namespace Gem_Tools_Controller.Gem_Tools
{
    public class CForm_Role_Controller
    {
        private void MapForm_Role(DataRow p_row, CForm_Role p_objDes)
        {
            DataTable p_dt = p_row.Table;

            if (p_dt.Columns.Contains("Auto_ID"))
                p_objDes.Auto_ID = CUtility.Convert_To_Int32(p_row["Auto_ID"]);

            if (p_dt.Columns.Contains("Staff_ID"))
                p_objDes.Staff_ID = CUtility.Convert_To_Int32(p_row["Staff_ID"]);

            if (p_dt.Columns.Contains("Form_Tree_ID"))
                p_objDes.Form_Tree_ID = CUtility.Convert_To_Int32(p_row["Form_Tree_ID"]);

            if (p_dt.Columns.Contains("Is_Show"))
                p_objDes.Is_Show = CUtility.Convert_To_Int32(p_row["Is_Show"]);

            if (p_dt.Columns.Contains("Is_Add"))
                p_objDes.Is_Add = CUtility.Convert_To_Int32(p_row["Is_Add"]);

            if (p_dt.Columns.Contains("Is_Update"))
                p_objDes.Is_Update = CUtility.Convert_To_Int32(p_row["Is_Update"]);

            if (p_dt.Columns.Contains("Is_Delete"))
                p_objDes.Is_Delete = CUtility.Convert_To_Int32(p_row["Is_Delete"]);

            if (p_dt.Columns.Contains("Created"))
                p_objDes.Created = CUtility.Convert_To_DateTime(p_row["Created"]);

            if (p_dt.Columns.Contains("Created_By"))
                p_objDes.Created_By = CUtility.Convert_To_String(p_row["Created_By"]);

            if (p_dt.Columns.Contains("Deleted"))
                p_objDes.Deleted = CUtility.Convert_To_Int32(p_row["Deleted"]);

            if (p_dt.Columns.Contains("Last_Updated"))
                p_objDes.Last_Updated = CUtility.Convert_To_DateTime(p_row["Last_Updated"]);

            if (p_dt.Columns.Contains("Last_Updated_By"))
                p_objDes.Last_Updated_By = CUtility.Convert_To_String(p_row["Last_Updated_By"]);

        }

        public IList<CForm_Role> List_Form_Role()
        {
            IList<CForm_Role> v_arrForm_Role = new List<CForm_Role>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_List_Form_Role");

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CForm_Role v_objForm_Role = new CForm_Role();
                    MapForm_Role(v_row, v_objForm_Role);
                    v_arrForm_Role.Add(v_objForm_Role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrForm_Role;
        }

        public IList<CForm_Role> A003_List_Form_Role_By_Staff_ID(int p_iStaff_ID)
        {
            IList<CForm_Role> v_arrForm_Role = new List<CForm_Role>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "A003_sp_sel_List_Form_Role_From_Staff_ID", p_iStaff_ID);

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CForm_Role v_objForm_Role = new CForm_Role();
                    MapForm_Role(v_row, v_objForm_Role);
                    v_arrForm_Role.Add(v_objForm_Role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrForm_Role;
        }

        public CForm_Role Get_Form_Role_By_ID(int p_iID)
        {
            CForm_Role v_objForm_Role = null;
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_Get_Form_Role_By_ID", p_iID);

                if (v_dt.Rows.Count > 0)
                {
                    v_objForm_Role = new CForm_Role();
                    MapForm_Role(v_dt.Rows[0], v_objForm_Role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_objForm_Role;
        }

        public CForm_Role Get_Form_Role_By_Staff_ID_And_Form_Tree_ID(int p_iStaff_ID, int Form_Tree_ID)
        {
            CForm_Role v_objForm_Role = null;
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "0_sp_sel_Get_Form_Role_By_Staff_ID_And_Form_Tree_ID", p_iStaff_ID, Form_Tree_ID);

                if (v_dt.Rows.Count > 0)
                {
                    v_objForm_Role = new CForm_Role();
                    MapForm_Role(v_dt.Rows[0], v_objForm_Role);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_objForm_Role;
        }

        public int Insert_Form_Role(CForm_Role p_objData)
        {
            int v_iRes = CConst.INT_VALUE_NULL;

            try
            {
                v_iRes = Convert.ToInt32(CSqlHelper.ExecuteScalar(CConfig.g_strGem_Tools_Conn_String, "sp_ins_Form_Role",
                            p_objData.Staff_ID, p_objData.Form_Tree_ID, p_objData.Is_Show, p_objData.Is_Add, p_objData.Is_Update, p_objData.Is_Delete, p_objData.Last_Updated_By).ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }


            return v_iRes;
        }

        public void Update_Form_Role(CForm_Role p_objData)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_upd_Form_Role",
                            p_objData.Auto_ID, p_objData.Staff_ID, p_objData.Form_Tree_ID, p_objData.Is_Show, p_objData.Is_Add, p_objData.Is_Update, p_objData.Is_Delete, p_objData.Last_Updated_By);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete_Form_Role(int p_iID, string p_strLast_Updated_User)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_del_Form_Role", p_iID, p_strLast_Updated_User);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
